Ext.namespace('CMS');
/*
 // this is the basic structure of a json CLASS
 CMS.EntityGrid = function(str){
 return{
 tibi : str,
 getTibi: function(){
 alert("This is a method");
 return this.tibi;
 }
 }
 }
 */
function GetEntityStore(id){
    alert("step1");
    Ext.Ajax.request({
        url: 'server/EntityItems.php?__id=' + this.entityId,
        success: SetStoreData
    });
}

function renderGrid(response){
    temp = eval(response.responseText);
    storeData = eval(temp[0]);
    storeReader = eval(temp[1]);
    storeItems = eval(temp[2]);
    myReader = new Ext.data.ArrayReader({}, storeReader);
    entityGrid = new CMS.EntityGrid({
        entityId: 1,
        
        store: new Ext.data.Store({
            data: storeData,
            reader: myReader
        }),
        columns: storeItems,
        
        viewConfig: {
            forceFit: true
        },
        renderTo: 'content',
        title: 'My First Grid',
        width: 500,
        autoHeight: true,
        frame: true
    });
    
    entityGrid.getSelectionModel().selectFirstRow();
}

CMS.EntityGrid = Ext.extend(Ext.grid.GridPanel, {
    entityId: 0,
    initComponent: function(){
        Ext.apply(this, {
            id: this.Id,
            title: "MiGrid",
            sm: new Ext.grid.RowSelectionModel({
                singleSelect: true
            }),
            stripeRows: true,
            width: 500,
            viewConfig: {
                forceFit: true
            }
        
        });
        
        //Required
        CMS.EntityGrid.superclass.initComponent.apply(this, arguments);
        //alert(this.columnModel);
    },
    onRender: function(){
        CMS.EntityGrid.superclass.onRender.apply(this, arguments);
    }
});

/*
 BKP BKP BKP
 CMS.EntityGrid = Ext.extend(Ext.grid.GridPanel, {
 entityId : 0,
 storeData :0,
 storeReader:0,
 storeItems:0,
 initComponent : function(){
 //alert(colModel);
 Ext.Ajax.request({
 url: 'server/json/entity.php?__id='+1+'&task=storeData',
 success: SetStoreData
 });
 function SetStoreData(response)
 {
 alert("GOGO");
 var temp = eval(response.responseText);
 storeData = eval(temp[0]);
 storeReader = eval(temp[1]);
 storeItems = eval(temp[2]);
 myReader = new Ext.data.ArrayReader({}, storeReader);
 Ext.apply(this, {
 id			: this.Id,
 store: new Ext.data.Store({
 data: storeData,
 reader: myReader
 }),
 columns		: this.storeItems,
 title		: "MiGrid",
 sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
 stripeRows	: true,
 width		: 500,
 viewConfig	:{
 forceFit: true
 }
 
 });
 }
 //Required
 CMS.EntityGrid.superclass.initComponent.apply(this, arguments);
 //alert(this.columnModel);
 },
 onRender: function(){
 CMS.EntityGrid.superclass.onRender.apply(this, arguments);
 }
 });
 */
/*
 Trash
 columnModel : "gol",
 getStoreData : function(){
 var storeData;
 Ext.Ajax.request({
 url: 'server/EntityItems.php?__id='+this.entityId,
 success: SetStoreData
 });
 function SetStoreData(response) {
 r = response.responseText;
 alert(r);
 var gridDefinition = eval(r);
 var myData = eval(gridDefinition[1]);
 var storeDef = eval(gridDefinition[2]);
 storeData = new Ext.data.SimpleStore({
 fields: storeDef
 });
 storeData.loadData(myData);
 return storeData;
 }
 return storeData;
 },
 getColumnModel : function(){
 //var columnModel;
 Ext.Ajax.request({
 url: 'server/EntityItems.php?__id='+this.entityId,
 success: setColumnModel
 });
 function setColumnModel(response) {
 r = response.responseText;
 var gridDefinition = eval(r);
 //alert(gridDefinition[0]);
 this.columnModel = eval(gridDefinition[0]);
 //alert(columnModel);
 colModel = columnModel;
 }
 },
 storeData : "",
 Ext.Ajax.request({
 url: 'server/EntityItems.php?__id='+this.entityId,
 success: function(response) {
 r = response.responseText;
 //alert(r);
 var gridDefinition = eval(r);
 var gridColumns = eval(gridDefinition[0]);
 var myData = eval(gridDefinition[1]);
 
 var storeDef = eval(gridDefinition[2]);
 //alert(myData);
 //alert(myData);
 this.storeData = new Ext.data.SimpleStore({
 fields: storeDef
 });
 //this.storeData.loadData(myData);
 //this.columnsData = new Ext.grid.ColumnModel(gridColumns);
 }
 });
 */
Ext.reg('entitygrid', CMS.EntityGrid);
